package it.unipi.di.rasip.gui.chat;

import it.unipi.di.rasip.Rasip;
import it.unipi.di.rasip.RasipUser;
import it.unipi.di.rasip.gui.RASDisplayable;
import it.unipi.di.rasip.gui.WelcomeCanvas;
import it.unipi.di.rasip.protocol.SipServer;
import it.unipi.di.rasip.utils.Color;
import it.unipi.di.rasip.utils.IconsFactory;
import it.unipi.di.rasip.utils.RasipConstant;
import it.unipi.di.rasip.utils.RasipLabel;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.sip.SipAddress;

public class ChatCreator extends RASDisplayable implements CommandListener {
	private static ChatCreator _instance;
	private static final int SKYPE_ITEM_INDEX = 1;
	
	public static ChatCreator getInstance() {
		if (null == _instance) {
			_instance = new ChatCreator();
		}
		return _instance;
	}
	
	Command createChatCmd;
	Command backCmd;
	Command backToChatManagerCmd;
	TextField usernameTf;
	TextField remoteAddrTf;
	private ChoiceGroup protocolCG;
	private RasipLabel alert;
	private Form form;
	
	
	
	private ChatCreator() {
		//SipServer.getInstance().start();
		form = new Form("Nuova chat");
		usernameTf = new TextField("Username", "sip:paolo@10.0.0.1:5080", 50, TextField.ANY);
		form.append(usernameTf);
		
		remoteAddrTf = new TextField("Indirizzo SIP remoto", "sip:uby@10.0.0.5:5080", 200, TextField.ANY);
		form.append(remoteAddrTf);
		
		protocolCG = new ChoiceGroup ("Protocol", Choice.EXCLUSIVE );
		protocolCG.append("Sip", IconsFactory.createIcon(IconsFactory.SIP_ICON));
		protocolCG.append("Skype", IconsFactory.createIcon(IconsFactory.SKYPE_ICON));
		form.append(protocolCG);
		
		alert = new RasipLabel(null);
		alert.setTextColor(Color.RED);
		alert.setBgColor(Color.CYAN);
		alert.setIcon(IconsFactory.createIcon(IconsFactory.ALERT));
		
		createChatCmd = new Command("Inizia", Command.OK, 1);
		form.addCommand(createChatCmd);
		backCmd = new Command("Esci", Command.EXIT, 2);
		form.addCommand(backCmd);
		backToChatManagerCmd = new Command("Torna alle chat", Command.BACK, 2);
		form.setCommandListener(this);
		
		alert.setVisible(false);
		form.append(alert);
		
		displayable = form;
	}

	public void commandAction(Command cmd, Displayable d) {
		alert.setVisible(false);
		
		if(cmd == createChatCmd) {
			if (protocolCG.getSelectedIndex() == SKYPE_ITEM_INDEX) {
				alert.setText("Protocollo non supportato");
				alert.setVisible(true);
			} else {
				System.out.println("CHAT_MANAGER");
				ChatManager cm = ChatManager.getInstance();
				String localURI = usernameTf.getString();
				String remoteURI = remoteAddrTf.getString();
				if(localURI.length() == 0) { //Username vuoto
					alert.setText("Inserire una username");
					alert.setVisible(true);
					return;
				}
				// Verifica se i dati in input sono conformi alle specifiche
				SipAddress saL = null;
				try {
					saL = new SipAddress(localURI);
					SipAddress saR = new SipAddress(remoteURI);

				} catch (Exception e) {
					alert.setText("PROTO:USER@HOST:PORTA");
					alert.setVisible(true);
					return;
				}
				RasipUser lu = RasipUser.rasipUserFromURI(saL);
				RasipUser.setLocalUser(lu);
				
				if(remoteURI.length() == 0) { //Indirizzo remoto vuoto
					alert.setText("Inserire un indirizzo remoto");
					alert.setVisible(true);
					return;
				}
				RasipUser ru = RasipUser.rasipUserFromURI(remoteURI);
				cm.newChat(ru, lu.getUsername());
				cm.show();
			}
			//Aggiungo un il comanto per tornare alle chat
			this.displayable.addCommand(backToChatManagerCmd);
		} else if (cmd == backCmd) {
			Rasip.getInstance().notifyDestroyed();
		} else if (cmd == backToChatManagerCmd) {
			ChatManager.getInstance().show();
		}
	}
}
